package com.hsq.androidproject.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyListViewFindTopicsAdapter;
import com.hsq.androidproject.entity.find.FindMineTL;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class QXfsFragment extends Fragment {//粉丝专区

    private PullToRefreshListView lv_home;
    //当前页数
    private int curPage = 1;
    private View footView;
    private MyListViewFindTopicsAdapter adapter;
    private List<FindMineTL.TopicsBean> topicsBean;
    private List<FindMineTL.TopicsBean> topicsBean1;
    private String path;
    private int sid;

    public QXfsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_qxfs, container, false);
        Bundle bundle = getArguments();    /////ShowHotStartsActivity
        sid = bundle.getInt("sid", 0);
        lv_home = (PullToRefreshListView) view.findViewById(R.id.lv_home);
        //String path = "http://api.hanju.koudaibaobao.com/bbs/api/forum/starTopics?sid=" + sid + "&page=" + curPage;
        addData1();
        lv_home.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { //下拉刷新
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                addData1();
            }
        });
        lv_home.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                requestData(curPage + 1);
            }
        });
        return view;
    }

    private void addData1() {
        path = "http://api.hanju.koudaibaobao.com/bbs/api/forum/starTopics?sid=" + sid + "&page=" + curPage;
        OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                FindMineTL findMineTL = new Gson().fromJson(response, FindMineTL.class);
                FindMineTL.BoardBean board = findMineTL.getBoard();
                topicsBean = findMineTL.getTopics();

                adapter = new MyListViewFindTopicsAdapter(topicsBean, board.getTitle(), getContext());
                lv_home.setAdapter(adapter);
                lv_home.onRefreshComplete();
            }
        });
    }

    private void requestData(final int curPage) {
        //初始化底部加载更多布局
        footView = View.inflate(getContext(), R.layout.footview_loading, null);
        path = "http://api.hanju.koudaibaobao.com/bbs/api/forum/topicsV2?bid=" + sid + "&page=" + curPage;
        if (curPage == 1) {
            lv_home.onRefreshComplete();
        } else {
            OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    addFootView(lv_home, footView);
                    FindMineTL findMineTL = new Gson().fromJson(response, FindMineTL.class);
                    topicsBean1 = findMineTL.getTopics();    //  得到集合数据
                    for (int i = 0; i < topicsBean1.size(); i++) {
                        topicsBean.add(topicsBean1.get(i));
                    }
                    //page为1 的时候 清理集合 ,否则不清理集合 调用下面的方法
                    adapter.notifyDataSetChanged();
                    lv_home.onRefreshComplete();  //完成
                }
            });
        }
    }

    private void addFootView(PullToRefreshListView lv_home, View footView) {
        ListView listView = lv_home.getRefreshableView();

        if (listView.getFooterViewsCount() == 1) {
            listView.addFooterView(footView);
        }
    }

}
